package com.example.project.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class StringUtil {

    public static boolean isEmpty(Object... values) {
        for (Object object : values) {
            if (isEmpty(getValue(object))) {
                return true;
            }
        }
        return false;
    }

    public static boolean isEmpty(String str) {
        return isNull(str) || isStringNull(str);
    }

    private static boolean isNull(String str) {
        return str == null || "[null]".equals(str) || "null".equals(str);
    }

    private static boolean isStringNull(String str) {
        return "undefined".equals(str) || "[]".equals(str) || str.trim().length() == 0;
    }

    public static boolean isEmpty(Collection<?> col) {
        return col == null || col.isEmpty();
    }

    public static boolean isEmpty(String[] strArr) {
        return strArr == null || strArr.length == 0;
    }


    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isNotEmpty(Collection<?> col) {
        return !isEmpty(col);
    }

    /**
     * 是否是全角字符，可以引申为判断一个字符串是否是中文，中文是全角字符
     *
     * @param str
     * @return
     */
    public static String getValue(Object str) {
        if (str == null)
            return "";
        return str.toString();
    }

    /**
     * 数组拼接成字符串
     * @param col 数组
     * @param delimit 隔开符
     * @return
     */
    public static String join(Collection<?> col, String delimit) {
        if (col.isEmpty())
            return "";
        StringBuilder sb = new StringBuilder();
        Iterator<?> iter = col.iterator();
        if (iter.hasNext())
            sb.append(iter.next().toString());
        while (iter.hasNext()) {
            sb.append(delimit);
            sb.append(iter.next().toString());
        }
        return sb.toString();
    }

    /**
     * 对象拼接成字符串
     * @param map 对象
     * @param delimit 隔开符
     * @return
     */
    public static String join(Map map, String delimit) {
        StringBuilder sb = new StringBuilder();
        Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
        if (iter.hasNext()) {
            Map.Entry<String, String> entry = iter.next();
            if (isNotEmpty(entry.getValue()))
                sb.append(entry.getValue());
            else
                sb.append(entry.getKey());
        }
        while (iter.hasNext()) {
            sb.append(delimit);
            Map.Entry<String, String> entry = iter.next();
            if (isNotEmpty(entry.getValue()))
                sb.append(entry.getValue());
            else
                sb.append(entry.getKey());
        }
        return sb.toString();
    }

    /**
     * 对象拼接成字符串
     * @param params 对象
     * @return
     */
    public static String join(Object... params) {
        StringBuilder sb = new StringBuilder();
        for (Object object:params)
            sb.append(object);
        return sb.toString();
    }

    /**
     * 随机码组合
     *
     * @param count 随机次数
     * @return 每次随机数的组合
     */
    public static String randomCodeNumber(final int count) {
        char[] codeSequence = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < count; i++) {
            code.append(codeSequence[(int) (codeSequence.length * Math.random())]);
        }
        return code.toString();
    }

    private StringUtil() {
        throw new IllegalStateException("Utility class");
    }
}
